package com.dakai.permission.controller;


import com.dakai.permission.common.R;
import com.dakai.permission.entity.SysUser;
import com.dakai.permission.service.ISysUserService;
import com.dakai.permission.utils.JwtUtils;
import com.dakai.permission.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dakai
 * @since 2022-10-02
 */
@RestController
@RequestMapping("test")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @RequestMapping("/user/list")
    public R userList(@RequestHeader(required = false) String token) {
        if (StringUtil.isNotEmpty(token)) {
            Map<String, Object> resutlMap = new HashMap<>();
            List<SysUser> userList = iSysUserService.list();
            resutlMap.put("userList", userList);
            return R.ok(resutlMap);
        } else {
            return R.error(401, "没有权限访问");
        }

    }

    @RequestMapping("/login")
    public R login() {
        String token = JwtUtils.genJwtToken("java1234");
        return R.ok().put("token", token);
    }
}
